package com.tedu.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * Created by IntelliJ IDEA.
 *
 * @author zq
 * @version 1.0
 * @create 2024-07-30 11:37
 * @since jdk1.8
 */
public class JDBCDemo01 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //创建数据库驱动对象
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获取数据库连接对象
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tedu", "root", "root");
        //创建SQL语句执行对象
        Statement statement = conn.createStatement();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();
        System.out.println("请输入昵称");
        String nickname = sc.nextLine();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        //执行SQL语句
        int count = statement.executeUpdate("insert into userinfo(username,password,nickname,age) " +
                "values('"+username+"','"+password+"','"+nickname+"','"+age+"')");
        if (count > 0) {
            System.out.println("插入成功");
        } else {
            System.out.println("插入失败");
        }

    }
}
